package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.user.User;
import com.qingcheng.service.user.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("/sendSms")
    public Result sendSms(@RequestParam(name = "phone") String phone) {
        Result result = new Result();
        //验证手机号是否格式正确的

        try {
            userService.sendSms(phone);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(1);
            result.setMessage(e.getMessage());
        }

        return result;
    }

    @PostMapping("/save")
    public Result registerUser(String smsCode, @RequestParam User user) {
        Result result = new Result();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        try {
            userService.registerUser(smsCode, user);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(1);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
